package xyz.yanghaijia.controller;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.aztec.AztecWriter;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.datamatrix.DataMatrixWriter;
import com.google.zxing.oned.*;
import com.google.zxing.pdf417.PDF417Writer;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import util.AlarmUtil;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;

public class Controller {

    @FXML
    private ComboBox<String> typeSelector;

    @FXML
    private GridPane basement;

    @FXML
    private Button filePick;

    @FXML
    private GridPane fileView;

    @FXML
    private GridPane typeView;

    @FXML
    private GridPane textView;

    @FXML
    private GridPane codeSizeView;

    @FXML
    private Button generate;

    @FXML
    private ComboBox<String> barcodeType;

    @FXML
    private TextField width;

    @FXML
    private TextArea text;


    private File file;

    @FXML
    public void initialize() {
        ObservableList<String> selections = FXCollections.observableArrayList("纯文本", "文件");
        typeSelector.setItems(selections);
//        basement.setPrefWidth(120);


        ObservableList<String> barcodes=FXCollections.observableArrayList("二维码","阿兹特克二维码","条形码","CODE39条码","CODE93条码","CODE128条码","二维码:数据矩阵","EAN-8条码","EAN-13条码","ITF条码");
        barcodes.addAll("二维码:MAXI","PDF417条码","RSS-14条码","RSS扩展条码","通用商品条码A","通用商品条码E","通用-EAN条码");
        barcodeType.setItems(barcodes);



        filePick.setOnAction(v -> {
            FileChooser fileChooser = new FileChooser();
            fileChooser.setTitle("选择想要转换的文件");
            file = fileChooser.showOpenDialog(new Stage());
        });

        typeSelector.setOnAction(v -> {
            switch (typeSelector.getValue()) {
                case "纯文本":
                    textView.disableProperty().setValue(false);
                    fileView.disableProperty().setValue(true);
                    break;
                case "文件":
                    textView.disableProperty().setValue(true);
                    fileView.disableProperty().setValue(false);
                    break;
                default:
                    break;
            }
        });

        generate.setOnAction(v->{
            Map<EncodeHintType,Object> hints=new HashMap<>();
            hints.put(EncodeHintType.CHARACTER_SET,"utf-8");
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            if ("纯文本".equals(typeSelector.getValue())) {
                if (barcodeType.getValue().isEmpty())
                {
                    try {
                        BitMatrix qw = new QRCodeWriter().encode(text.getText(), BarcodeFormat.QR_CODE, Integer.parseInt(width.getText()), Integer.parseInt(width.getText()), hints);
                        BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(qw, new MatrixToImageConfig(Color.BLACK.getRGB(), Color.WHITE.getRGB()));
                        try {
                            ImageIO.write(bufferedImage,"png",new File("./"+ Instant.now().toEpochMilli()+".png"));
                            AlarmUtil.dialogCreator(2,"成功","二维码成功生成",null);
                        } catch (IOException e) {
                            AlarmUtil.dialogCreator(3,"错误","二维码未成功生成",null);
                        }

                    }
                    catch (Exception we){
                        AlarmUtil.dialogCreator(3,"错误","二维码未成功生成",null);
                    }
                }
                else {
                    try {


                        BitMatrix qw;

                        switch (barcodeType.getValue()){
                            case "二维码":
                               qw=new QRCodeWriter().encode(text.getText(), getBarcodeFormat(barcodeType.getValue()), Integer.parseInt(width.getText()), Integer.parseInt(width.getText()), hints);
                            break;
                               case "阿兹特克二维码":
                                   hints.remove(EncodeHintType.ERROR_CORRECTION);
                                   hints.put(EncodeHintType.AZTEC_LAYERS,2);
                               qw= new AztecWriter().encode(text.getText(), getBarcodeFormat(barcodeType.getValue()), Integer.parseInt(width.getText()), Integer.parseInt(width.getText()), hints);
                            break;
                               case "条形码":
                            qw= new CodaBarWriter().encode(text.getText(), getBarcodeFormat(barcodeType.getValue()), Integer.parseInt(width.getText()), Integer.parseInt(width.getText()), hints);
                            break;
                            case "CODE39条码":
                                qw= new Code39Writer().encode(text.getText(), getBarcodeFormat(barcodeType.getValue()), Integer.parseInt(width.getText()), Integer.parseInt(width.getText()), hints);
                                break;
                            case "CODE93条码":
                                qw= new Code93Writer().encode(text.getText(), getBarcodeFormat(barcodeType.getValue()), Integer.parseInt(width.getText()), Integer.parseInt(width.getText()), hints);
                                break;
                            case "CODE128条码":
                                qw= new Code128Writer().encode(text.getText(), getBarcodeFormat(barcodeType.getValue()), Integer.parseInt(width.getText()), Integer.parseInt(width.getText()), hints);
                                break;
                            case "二维码:数据矩阵":
                                qw= new DataMatrixWriter().encode(text.getText(), getBarcodeFormat(barcodeType.getValue()), Integer.parseInt(width.getText()), Integer.parseInt(width.getText()), hints);
                                break;
                            case "EAN-8条码":
                                qw= new EAN8Writer().encode(text.getText(), getBarcodeFormat(barcodeType.getValue()), Integer.parseInt(width.getText()), Integer.parseInt(width.getText()), hints);
                                break;
                            case "EAN-13条码":
                                qw= new EAN13Writer().encode(text.getText(), getBarcodeFormat(barcodeType.getValue()), Integer.parseInt(width.getText()), Integer.parseInt(width.getText()), hints);
                                break;
                            case "ITF条码":
                                qw= new ITFWriter().encode(text.getText(), getBarcodeFormat(barcodeType.getValue()), Integer.parseInt(width.getText()), Integer.parseInt(width.getText()), hints);
                                break;
//                            case "二维码:MAXI":
//                                qw= new ().encode(text.getText(), getBarcodeFormat(barcodeType.getValue()), Integer.parseInt(width.getText()), Integer.parseInt(width.getText()), hints);
//                                break;
                            case "PDF417条码":
                                qw= new PDF417Writer().encode(text.getText(), getBarcodeFormat(barcodeType.getValue()), Integer.parseInt(width.getText()), Integer.parseInt(width.getText()), hints);
                                break;
//                            case "RSS-14条码":
//                                qw= new ().encode(text.getText(), getBarcodeFormat(barcodeType.getValue()), Integer.parseInt(width.getText()), Integer.parseInt(width.getText()), hints);
//                                break;
//                            case "RSS扩展条码":
//                                qw= new ().encode(text.getText(), getBarcodeFormat(barcodeType.getValue()), Integer.parseInt(width.getText()), Integer.parseInt(width.getText()), hints);
//                                break;
                            case "通用商品条码A":
                                qw= new UPCAWriter().encode(text.getText(), getBarcodeFormat(barcodeType.getValue()), Integer.parseInt(width.getText()), Integer.parseInt(width.getText()), hints);
                                break;
                            case "通用商品条码E":
                                qw= new UPCEWriter().encode(text.getText(), getBarcodeFormat(barcodeType.getValue()), Integer.parseInt(width.getText()), Integer.parseInt(width.getText()), hints);
                                break;
                                case "通用-EAN条码":
//                                    qw= new .encode(text.getText(), getBarcodeFormat(barcodeType.getValue()), Integer.parseInt(width.getText()), Integer.parseInt(width.getText()), hints);
//                                    break;
                            default:
                                qw= new QRCodeWriter().encode(text.getText(), getBarcodeFormat(barcodeType.getValue()), Integer.parseInt(width.getText()), Integer.parseInt(width.getText()), hints);
                                break;
                        }


                        BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(qw, new MatrixToImageConfig(Color.BLACK.getRGB(), Color.WHITE.getRGB()));
                        try {
                            ImageIO.write(bufferedImage,"png",new File("./"+ Instant.now().toEpochMilli()+".png"));
                            AlarmUtil.dialogCreator(2,"成功","二维码成功生成",null);
                        } catch (IOException e) {
                            AlarmUtil.dialogCreator(3,"错误","二维码未成功生成",null);
                        }

                    }
                    catch (Exception we){
                        AlarmUtil.dialogCreator(3,"错误","二维码未成功生成",null);
                        we.printStackTrace();
                    }
                }
                }

            else if ("文件".equals(typeSelector.getValue())){
                AlarmUtil.dialogCreator(1,"很抱歉","暂时不提供此类服务",null);
            }
            else{
                AlarmUtil.dialogCreator(1,"注意","请先指定要转换的类型",null);
            }

        });





    }
    
    private BarcodeFormat getBarcodeFormat(String chinese)
    {
        switch (chinese){
            case "二维码":
                return BarcodeFormat.QR_CODE;
            case "阿兹特克二维码":
                return BarcodeFormat.AZTEC;
            case "条形码":
                return BarcodeFormat.CODABAR;
            case "CODE39条码":
                return BarcodeFormat.CODE_39;
            case "CODE93条码":
                return BarcodeFormat.CODE_93;
            case "CODE128条码":
                return BarcodeFormat.CODE_128;
            case "二维码:数据矩阵":
                return BarcodeFormat.DATA_MATRIX;
            case "EAN-8条码":
                return BarcodeFormat.EAN_8;
            case "EAN-13条码":
                return BarcodeFormat.EAN_13;
                case "ITF条码":
                    return BarcodeFormat.ITF;
            case "二维码:MAXI":
                return BarcodeFormat.MAXICODE;
            case "PDF417条码":
                return BarcodeFormat.PDF_417;
            case "RSS-14条码":
                return BarcodeFormat.RSS_14;
            case "RSS扩展条码":
                return BarcodeFormat.RSS_EXPANDED;
            case "通用商品条码A":
                return BarcodeFormat.UPC_A;
            case "通用商品条码E":
                return BarcodeFormat.UPC_E;
            case "通用-EAN条码":
                return BarcodeFormat.UPC_EAN_EXTENSION;
                default:
                    return BarcodeFormat.QR_CODE;
    }
    }


}
